package cn.commander.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: 时间工具类
 * @Created by:
 * @Date: 2016/4/16 18:23
 * @Version: 1.0
 */
public abstract class DateUtils {
	private static String currentTime;
	private static String currentDate;

	public static final String Y_M_D = "yyyy-MM-dd";
	public static final String YMD = "yyyyMMdd";
	public static final String Y_M_D_HMS = "yyyy-MM-dd HH:mm:ss";
	public static final String YMDHMSS = "yyyyMMddHHmmssSSS";

	private DateUtils() {
	}

	public static Date now() {
		return new Date();
	}

	/**
	 * 格式化时间
	 *
	 * @param date       时间
	 * @param formatType 格式化类型 如：yyyy-MM-dd
	 * @return
	 */
	public static Object formatDate(Object date, String formatType) {
		SimpleDateFormat sd = new SimpleDateFormat(formatType);
		if (date instanceof Date) {
			return sd.format((Date) date);
		} else if (date instanceof String) {
			try {
				return sd.parse((String) date);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 获取指定某天的起始时间
	 *
	 * @param date 2017-09-18
	 * @return 2017-09-18 00:00:00
	 */
	public static Date getStartDate(Date date) {
		String dataStr = DateUtils.formatDate(date, DateUtils.Y_M_D) + " 00:00:00";
		return (Date) formatDate(dataStr, DateUtils.Y_M_D_HMS);
	}

	/**
	 * 获取指定某天的结束时间
	 *
	 * @param date 2017-09-18
	 * @return 2017-09-18 23:59:59
	 */
	public static Date getEndDate(Date date) {
		String dataStr = DateUtils.formatDate(date, DateUtils.Y_M_D) + " 23:59:59";
		return (Date) formatDate(dataStr, DateUtils.Y_M_D_HMS);
	}

	/**
	 * 获取当天开始时间
	 *
	 * @return
	 */
	public static Date getTodayStartDate() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	/**
	 * 获取当天结束时间
	 *
	 * @return
	 */
	public static Date getTodayEndDate() {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}

	/**
	 * 获取当前月的第一天
	 *
	 * @return
	 */
	public static Date getCurrentMonthFirstDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取当前月的最后一天
	 *
	 * @return
	 */
	public static Date getCurrentMonthLastDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}

	/**
	 * 获取当前时间之前或之后几分钟
	 *
	 * @param minute
	 * @return
	 */
	public static Date getTimeByMinute(int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTime();
	}

	/**
	 * 获取当月的 天数
	 */
	public static int getCurrentMonthDay() {
		Calendar a = Calendar.getInstance();
		a.set(Calendar.DATE, 1);
		a.roll(Calendar.DATE, -1);
		int maxDate = a.get(Calendar.DATE);
		return maxDate;
	}

	/**
	 * 根据年 月 获取对应的月份 天数
	 */
	public static int getDaysByYearMonth(int year, int month) {
		Calendar a = Calendar.getInstance();
		a.set(Calendar.YEAR, year);
		a.set(Calendar.MONTH, month - 1);
		a.set(Calendar.DATE, 1);
		a.roll(Calendar.DATE, -1);
		int maxDate = a.get(Calendar.DATE);
		return maxDate;
	}

	/**
	 * 得到当前的年份 返回格式:yyyy
	 *
	 * @return String
	 */
	public static String getCurrentYear() {
		Date nowDate = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
		return formatter.format(nowDate);
	}

	/**
	 * 得到当前的月份 返回格式:MM
	 *
	 * @return String
	 */
	public static String getCurrentMonth() {
		Date nowDate = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("MM");
		return formatter.format(nowDate);
	}

	/**
	 * 得到当前的时间，精确到毫秒,共19位 返回格式:yyyy-MM-dd HH:mm:ss
	 *
	 * @return String
	 */
	public static String getCurrentTime() {
		Date NowDate = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		currentTime = formatter.format(NowDate);
		return currentTime;
	}
}
